package com.xiaofeng.pochi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.enums.StateEnums;
import com.xiaofeng.pochi.mapper.SysUserMapper;
import com.xiaofeng.pochi.mapper.SysUserRoleMapper;
import com.xiaofeng.pochi.pojo.SysUser;
import com.xiaofeng.pochi.pojo.SysUserRole;
import com.xiaofeng.pochi.pojo.vo.SysUserVo;
import com.xiaofeng.pochi.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/07/22/22:37
 * @Description:
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public void updateLoginTime(String username) {
        sysUserMapper.updateLoginTime(username);
    }

    @Override
    public SysUser getByUsername(String username) {
        return sysUserMapper.getByUsername(username);
    }


//    @Override
//    public void saveUser(SysUser sysUser) {
//        //保存用户
//        this.baseMapper.insert(sysUser);
//    }

    @Override
    public void updateUser(SysUserVo sysUser) {

        SysUser user = new SysUser();
        BeanUtils.copyProperties(sysUser,user);
        this.baseMapper.updateById(user);
        // 不管前端有没有选择角色，我们都把旧的角色信息删掉，如果选择了，就再添加新的角色信息
        sysUserRoleMapper.deleteByUserId(user.getId());
        // 如果角色id存在，存入用户角色表
        if (sysUser.getSysRole() != null && sysUser.getSysRole().getRoleId() != null) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(user.getId());
            sysUserRole.setRoleId(sysUser.getSysRole().getRoleId());
            sysUserRoleMapper.insert(sysUserRole);
        }
    }

    @Override
    public void enable(Long id) {
        //先查再改
        SysUser sysUser = this.baseMapper.selectById(id);
        sysUser.setStatus(StateEnums.ENABLED.getCode());
        this.baseMapper.updateStatus(sysUser);
    }

    @Override
    public void disable(Long id) {
        //先查再改
        SysUser sysUser = this.baseMapper.selectById(id);
        sysUser.setStatus(StateEnums.NOT_ENABLE.getCode());
        this.baseMapper.updateStatus(sysUser);
    }

    @Override
    public Page<SysUser> getByPage(Page<SysUser> page) {
        // 设置默认的当前页和每页条数
        Integer pageNumber = page.getPageNumber();
        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
            page.setPageNumber(pageNumber);
        }
        List<SysUser> userList = sysUserMapper.getByPage(page);
        Integer totalCount = sysUserMapper.countByPage(page);
        page.setList(userList);
        page.setTotalCount(totalCount);
        return page;
    }


}
